package com.bjpowernode.service.impl;

import com.bjpowernode.mapper.TActivityMapper;
import com.bjpowernode.mapper.TClueMapper;
import com.bjpowernode.mapper.TCustomerMapper;
import com.bjpowernode.mapper.TTranMapper;
import com.bjpowernode.result.AxisData;
import com.bjpowernode.result.NameValueData;
import com.bjpowernode.result.OverViewData;
import com.bjpowernode.result.TimeValueData;
import com.bjpowernode.service.StatisticService;
import com.bjpowernode.util.NumberUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class StatisticServiceImpl implements StatisticService {

    @Resource
    private TActivityMapper tActivityMapper;

    @Resource
    private TClueMapper tClueMapper;

    @Resource
    private TCustomerMapper tCustomerMapper;

    @Resource
    private TTranMapper tTranMapper;

    @Override
    public OverViewData getOverViewData() {
        //目前在进行的市场活动数量  /  数据库中所有的市场活动总数
        Integer onGingActivityCount = tActivityMapper.selectOngoingActivity().size();

        Integer totalActivityCount = tActivityMapper.selectTotalActivityCount();

        //线索总数
        Integer totalClueCount = tClueMapper.selectTotalClueCount();

        //客户总数
        Integer totalCustomerCount = tCustomerMapper.selectTotalCustomerCount();

        //成功的交易额 / 总的交易额（包含成功和不成功的）
        BigDecimal successTranAmount = tTranMapper.selectSuccessTranAmount();

        BigDecimal totalTranAmount = tTranMapper.selectTotalTranAmount();
        ;

        return OverViewData.builder()
                .onGingActivityCount(onGingActivityCount)
                .totalActivityCount(totalActivityCount)
                .totalClueCount(totalClueCount)
                .totalCustomerCount(totalCustomerCount)
                .successTranAmount(successTranAmount)
                .totalTranAmount(totalTranAmount)
                .build();
    }

    @Override
    public List<NameValueData> getFunelData() {
        List<NameValueData> nameValueDataList = new ArrayList<>();
        /*
            [
              //数据值, 数据项名称
              { value: 100, name: '线索' },
              { value: 70, name: '客户' },
              { value: 45, name: '交易' },
              { value: 18, name: '成交' }
            ]
         */
        Integer clueCount = tClueMapper.selectTotalClueCount();
        NameValueData clueNameValueData = NameValueData.builder()
                .name("线索")
                //.value(clueCount)
                .value(100)
                .build();
        nameValueDataList.add(clueNameValueData);

        Integer customerCount = tCustomerMapper.selectTotalCustomerCount();
        NameValueData customerNameValueData = NameValueData.builder()
                .name("客户")
                .value(NumberUtil.compute(customerCount, clueCount)) // customerCount / clueCount
                .build();
        nameValueDataList.add(customerNameValueData);

        Integer tranCount = tTranMapper.selectTotalTranCount();
        NameValueData tranNameValueData = NameValueData.builder()
                .name("交易")
                .value(NumberUtil.compute(tranCount, clueCount))  // tranCount / clueCount
                .build();
        nameValueDataList.add(tranNameValueData);

        Integer successTranCount = tTranMapper.selectsuccessTranCount();
        NameValueData successTranNameValueData = NameValueData.builder()
                .name("成交")
                .value(NumberUtil.compute(successTranCount, clueCount)) // successTranCount / clueCount
                .build();
        nameValueDataList.add(successTranNameValueData);

        return nameValueDataList;
    }

    @Override
    public List<NameValueData> getPieData() {
        return tClueMapper.selectClueCountBySource();
    }

    @Override
    public Integer[] getBarData() {
        //查询每月举办了几个活动，最终返回的数据结构：[10, 0, 17, 22, 18, 0, 11]
        List<TimeValueData> timeValueDataList = tActivityMapper.selectActivityCountByMonth();

        /**
         * 1	2024-01
         * 7	2024-02
         * 4	2024-05
         * 3	2024-06
         * 2	2024-07
         *
         * 问题：3月、4月没有数据，没有数据的月份要补0
         *
         * 最终返回数据应该是7条数据：[1, 7, 0, 0, 4, 3, 2]
         */
        int currentMonth = LocalDate.now().getMonthValue();
        Integer[] result = new Integer[currentMonth];

        //数组下标
        int index = 0;
        for (int mon = 1; mon <= currentMonth; mon++) { //7个月，循环7次，每个月份都去查询出来的List中找一下有没有该月份的数据
            for (int i = 0; i < timeValueDataList.size(); i++) {
                TimeValueData timeValueData = timeValueDataList.get(i);
                //月份
                String month = timeValueData.getTime().startsWith("0") ?
                        timeValueData.getTime().substring(1) : timeValueData.getTime();
                //月份对应的活动个数
                Integer value = timeValueData.getValue();

                if (month.equals(String.valueOf(mon))) {
                    result[index] = value;
                    break;
                } else {
                    result[index] = 0;
                }
            }
            index++;
        }
        return result;
    }

    @Override
    public AxisData getClueBarData() {
        //X轴的数据
        Integer[] xData = new Integer[LocalDate.now().lengthOfMonth()];
        for (int i = 1; i <= LocalDate.now().lengthOfMonth(); i++) {
            xData[i - 1] = i;
        }

        /**
         * 1	02
         * 2	07
         * 3	09
         * 2	11
         * 1	15
         * 2	18
         */
        //Y轴的数据
        List<TimeValueData> timeValueDataList = tClueMapper.selectClueCountByDay();

        int currentDay = LocalDate.now().getDayOfMonth();
        Integer yData[] = new Integer[currentDay];

        //数组下标
        int index = 0;
        for (int d = 1; d <= currentDay; d++) { //今天是18号，循环18次，每一天都去查询出来的List中找一下有没有该天的数据
            for (int i = 0; i < timeValueDataList.size(); i++) {
                TimeValueData timeValueData = timeValueDataList.get(i);
                //哪一天
                String day = timeValueData.getTime().startsWith("0") ?
                        timeValueData.getTime().substring(1) : timeValueData.getTime();
                //天对应的线索个数
                Integer value = timeValueData.getValue();

                if (day.equals(String.valueOf(d))) {
                    yData[index] = value;
                    break;
                } else {
                    yData[index] = 0;
                }
            }
            index++;
        }

        AxisData axisData = new AxisData();
        axisData.setXData(xData);
        axisData.setYData(yData);

        return axisData;
    }
}


